<?php
/*
This script is used to help automate the Football Pool site.
It is written to run everyday from a windows scheduled task, or
linux cron job at 12:30 everyday.
The script check to see if there is currently a game in progress,
if so it will gather a list of all games that start close to the current time,
i.e. 1:00, 4:05, 4:15, 8:30, etc....
It will then send an email to everyone displaying everyone's picks for the games that 
have already started and cannont be changed.
If not, it reloads itself at different intervals based on the day. (More
frequently on days with higher possability of games).

*/
echo "running...<br/>";
if ($GameTime == "")
{
}
else
{
require_once("../include/functions.php");
?>
<html>
  <head>
    <title>JoshHughes.com NFL Football Pool</title>
	<script type="text/javascript" src="scrape.js"></script>
  </head>
<body>
<?php
//Connect To Database
dbConnect();
$Weeknum = getWeek();

//check to make sure $Weeknum > 1 AND $Weeknum < 21
$Weeknum = checkWeekBounds($Weeknum);
echo "<h2>Week $Weeknum</h2>";

//Select user information from the Users table, if the user wants to be reminded
if($Weeknum > 17)
{
	$SQL = "SELECT ID, username, email FROM NFLUsers WHERE PlayOffs = 1 AND ID=1 ORDER BY username";
}
else
{
	$SQL = "SELECT ID, username, email FROM NFLUsers WHERE ID=1";
}
$GameTimeStr = date("g:i",mktime(substr($GameTime, 0, 2), substr($GameTime, 2, 2), substr($GameTime, 4, 2), date("m") , date("d"), date("Y")));
$GameDateStr = date("Ymd");
//Open Record Set using SQL Query
$Emailresult = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
//set up some email varibables
$adminmail = "<Football@joshhughes.com>";
$MailFrom = "Football@joshhughes.com";
$MailSubject = "Football Picks For Today's $GameTimeStr Game(s)";
$MailBody = "Here are the users picks for today's $GameTimeStr game(s):";
$HTMLMailBody = "Here are the users picks for today's $GameTimeStr game(s):<br/>(NP = \"No Pick Made\")<br/><br/>";
do{
	if (($Emailrow["email"] != "")&&($Emailrow["email"] != " ")) {   //add user's email to the list.
	$adminmail .= ", ".$Emailrow["username"]." <".$Emailrow["email"].">";
	}//end if ($Emailrow["email"] != "")
}while($Emailrow = mysql_fetch_array($Emailresult));

$HTMLMailBody .=  "<table bgcolor=\"#FFFFFF\" border=\"1\" callpadding=\"5\" cellspacing=\"0\" width=\"100\" align=\"left\">";
#$query="SELECT * FROM NFLSchedule Where ((WEEK=".$Weeknum.") AND (GameDate = ".$GameDateStr.") AND (GameTime = ".$GameTime.")) GROUP BY GameNum";
$query="SELECT * FROM NFLSchedule Where WEEK=".$Weeknum." GROUP BY GameNum";
$doQuery=mysql_query($query);
$numrows=mysql_num_rows($doQuery);
$num = 1;
if($numrows > 0)
{
	while($results=mysql_fetch_array($doQuery))
	{
		//Fill Arrays With Data
		$homeTeams[$num]=$results['Home'];
		$awayTeams[$num]=$results['Away'];
		$gamenums[$num]=$results['GameNum'];
		$num++;		
	}
	//Print Away Line
	$HTMLMailBody .=  "<tr bgcolor=\"#FFFFFF\"><td><b>Away</b></td>";
	foreach($awayTeams as $team)
	{
	  $HTMLMailBody .=  "<td width=\"5\" align=\"center\"><img src=\"http://football.joshhughes.com/images/small/".$team.".gif\"></td>";
	}
	
	//Print @ Line
	$HTMLMailBody .=  "<tr bgcolor=\"#FFFFFF\"><td>&nbsp;</td>";
	foreach($homeTeams as $team)
	{
	  $HTMLMailBody .=  "<td width=\"5\" align=\"center\">@</td>";
	}
	$HTMLMailBody .=  "</tr>";
	
	//Print Home Line
	$HTMLMailBody .=  "<tr bgcolor=\"#FFFFFF\" bordercolor=\"#172693\"><td><b>Home</b></td>";
	foreach($homeTeams as $team)
	{
	  $HTMLMailBody .=  "<td width=\"5\" align=\"center\"><img src=\"http://football.joshhughes.com/images/small/".$team.".gif\"></td>";
	}
	$HTMLMailBody .=  "</tr>";
	
	$cols = $numrows + 2;
	$HTMLMailBody .=  "<tr height=\"1\" bgcolor=\"#FFFFFF\"><td colspan=\"$cols\" bgcolor=\"#FF0000\"><span style=\"font-size: 8px;\"&nbsp;</td></tr>";
	
	//Get Data From Users Who Picked
	$UserPickedSQL = "SELECT NFLPicks.*, NFLUsers.username, NFLUsers.ID FROM NFLPicks, NFLUsers WHERE ((NFLPicks.Week = ".$Weeknum.") AND (NFLPicks.UserID = NFLUsers.ID))  ORDER BY NFLPicks.UserID ASC";
	$UserPickedresult = mysql_query($UserPickedSQL) or die("Cannot query the database.<br>" . mysql_error());
	$row = 0;
	do{
	
		if ($myrow["UserID"] != NULL)
		{
			//increment row for the row color
			$row++;
			$HTMLMailBody .=  "<tr bgcolor=\"".getrowColor($row)."\">";
			$HTMLMailBody .=  "<td><a href=\"members.php?ID=".$myrow["UserID"]."\" class=\"permLink\">".$myrow["username"]."</a></td>\n";
			$MailBody .= "\n\n".$myrow["username"]." Picked: \n";
			$weekScore = 0;
			for($j = 1; $j <= $numrows; $j++)
			{
				$pick = $myrow["Game".$gamenums[$j]];
				if($pick!="")
				{
					$HTMLMailBody .=  "<td width=\"5\" align=\"center\"><img src=\"http://football.joshhughes.com/images/small/".$pick.".gif\" /></td>\n";
					$MailBody .= "\t".$pick."\n";
				}
				else
				{
					$HTMLMailBody .=  "<td width=\"5\" align=\"center\">NP</td>\n";		
				}
			}
		}
		
	}while($myrow = mysql_fetch_array($UserPickedresult));
	
}
else
{
	print '<tr bgcolor=\"#FFFFFF\"><td colspan=\"$cols\" align=\"center\">No Games Set for This Week Yet.</td></tr>';
}
//Close table
$HTMLMailBody .=  "</table>";
$HTMLMailBody .= "<BR><BR><BR><font size=1> Users may &quot;Opt Out&quot; of this feature by changing their preferences using the \"Change Email Prefs\" link in the Log On Box....<BR>This feature is turned on by default at user registration.</font>";
$MailBody .= "\n\n\n Users may \"Opt Out\" of this feature by changing their preferences using the \"Change Email Prefs\" link in the Log On Box....\nThis feature is turned on by default at user registration.";

$MailTo = $adminmail;
//add From: header
$headers = "From: Football@joshhughes.com\r\n";

//specify MIME version 1.0
$headers .= "MIME-Version: 1.0\r\n";

//unique boundary
$boundary = uniqid("HTMLDEMO");

//tell e-mail client this e-mail contains//alternate versions
$headers .= "Content-Type: multipart/alternative" ."; boundary = $boundary\r\n\r\n";

//message to people with clients who don't
//understand MIME
$headers .= "This is a MIME encoded message.\r\n\r\n";

//plain text version of message
$headers .= "--$boundary\r\n" .
 "Content-Type: text/plain; charset=ISO-8859-1\r\n" .
 "Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($MailBody));

//HTML version of message
$headers .= "--$boundary\r\n" .
 "Content-Type: text/html; charset=ISO-8859-1\r\n" .
 "Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($HTMLMailBody));
//Send an Email to me notifying me of a request
#mail($MailTo, $MailSubject, "", $headers);  //message send
#mail($MailTo, $MailSubject, $MailBody, $MailFrom);  //message send

send_mail($MailTo, $HTMLMailBody, $MailSubject, "football@joshhughes.com", "Football Pool", false);

echo "<H1>MailTo: $MailTo</H1>";

echo $HTMLMailBody;

echo "<br/><br/>pickEmailer script was run at ". date("g:i");
}
?>

<div id="State"><br/></div>

</body>
</html>
